﻿using Supermarket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XLSupermarket.Financial.Wages
{
    public partial class WagesForm1 : Form
    {
        public WagesForm1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int textnumber = int.Parse(this.textnumber.Text);
                string sql = string.Format("select * from EP_information where EP_ID = '{0}'", textnumber);
                DataTable dt = DBHelper.FillData(sql);
                if (dt.Rows.Count > 0)
                {
                    string sql1 = string.Format("select * from EP_Salary where EP_ID = '{0}'", textnumber);
                    DataTable dt1 = DBHelper.FillData(sql1);
                    if (dt1.Rows.Count > 0)
                    {
                        string sql2 = string.Format("select ei.EP_Name 员工姓名,ei.EP_Phone 联系电话,ei.EP_Gender 性别,ei.EP_IDcard 身份证号,es.Bank_Card_Number 银行卡号,ei.Staff_Name 职位名称,Base_Pay 基础工资 from EP_Salary es,EP_information ei where es.EP_ID = ei.EP_ID and es.EP_ID = '{0}' order by es.Date_Of_issue desc", textnumber);
                        DataTable dt2 = DBHelper.FillData(sql2);
                        this.Employee_name.Text = dt2.Rows[0][0].ToString();
                        this.phone.Text = dt2.Rows[0][1].ToString();
                        this.Genders.Text = dt2.Rows[0][2].ToString();
                        this.ID.Text = dt2.Rows[0][3].ToString();
                        this.textBox3.Text = dt2.Rows[0][4].ToString();
                        this.Staff_Name.Text = dt2.Rows[0][5].ToString();
                        this.Basic_salaryBox.Text = dt2.Rows[0][6].ToString();
                    }
                    else
                    {
                        MessageBox.Show("薪资表没有该员工");
                        string sql3 = string.Format("select EP_Name 员工姓名,EP_Phone 联系电话,EP_Gender 性别,EP_IDcard 身份证号,Staff_Name 职位名称 from EP_information where EP_ID = {0}", textnumber);
                        DataTable dt3 = DBHelper.FillData(sql3);
                        this.Employee_name.Text = dt3.Rows[0][0].ToString();
                        this.phone.Text = dt3.Rows[0][1].ToString();
                        this.Genders.Text = dt3.Rows[0][2].ToString();
                        this.ID.Text = dt3.Rows[0][3].ToString();
                        this.Staff_Name.Text = dt3.Rows[0][4].ToString();
                    }
                }
                else
                {
                    MessageBox.Show("没有该员工");
                    Clear();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入员工编号");
                Clear();
            }

            
        }

        public void Clear()
        {
            this.Employee_name.Text = null;
            this.phone.Text = null;
            this.Genders.Text = null;
            this.textnumber.Text = null;
            this.ID.Text = null;
            this.Staff_Name.Text = null;
            this.textBox3.Text = null;
            this.comboBox1.SelectedIndex = 0;
            this.Basic_salaryBox.Text = null;
            this.textCommission.Text = null;
            this.textBox1.Text = null;
            this.textRemark.Text = null;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string EP_Number = this.textnumber.Text;
            string Hire_date = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string Bank_Card_Number = this.textBox3.Text;
            string Distribution_Method = this.comboBox1.Text;
            string Bonus = this.textBox1.Text;
            string Base_Pay = this.Basic_salaryBox.Text;
            string Commission = this.textCommission.Text;
            string Other = this.textRemark.Text;
            if (EP_Number=="")
            {
                MessageBox.Show("员工编号不能为空！！！");
            }
            else
            {
                if (Hire_date == "" )
                {
                    MessageBox.Show("发放日期不能为空！！！");
                }
                else
                {
                    if (Base_Pay == "")
                    {
                        MessageBox.Show("基本工资不能为空！！！");
                    }
                    else
                    {
                        if (Distribution_Method == "现金")
                        {
                            string sql = string.Format("insert into EP_Salary values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", EP_Number, Hire_date, Commission, Base_Pay, Bonus, Distribution_Method, Bank_Card_Number, Other);
                            int num = DBHelper.Edit(sql);
                            if (num > 0)
                            {
                                MessageBox.Show("保存成功");
                                Clear();
                            }
                            else
                            {
                                MessageBox.Show("保存失败");
                            }
                        }
                        if (Distribution_Method == "银行卡")
                        {
                            if (Bank_Card_Number == "")
                            {
                                MessageBox.Show("银行卡号必须填写");
                            }
                            else
                            {
                                string sql = string.Format("insert into EP_Salary values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", EP_Number, Hire_date, Commission, Base_Pay, Bonus, Distribution_Method, Bank_Card_Number, Other);
                                int num = DBHelper.Edit(sql);
                                if (num > 0)
                                {
                                    MessageBox.Show("保存成功");
                                    Clear();
                                }
                                else
                                {
                                    MessageBox.Show("保存失败");
                                }
                            }
                        }
                    }
                }
            }
            
        }


        private void WagesForm1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
        }
    }
}
